﻿//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺  
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net 设置信息实体类      ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                    ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone               ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------
using System.Collections.Generic;
using System.Reflection;

namespace Wedn.Net.Common
{
    /// <summary>
    /// 网站设置信息类
    /// </summary>
    public static class Setting
    {
        #region OldSetting
        //private static string _siteUrl;
        ///// <summary>
        ///// 站点地址
        ///// </summary>
        //public static string SiteUrl
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(_siteUrl))
        //            _siteUrl = GetSetting("SiteUrl");
        //        return _siteUrl;
        //    }
        //}
        //private static string _siteName;
        ///// <summary>
        ///// 站点名称
        ///// </summary>
        //public static string SiteName
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(_siteName))
        //            _siteName = GetSetting("SiteName");
        //        return _siteName;
        //    }
        //}
        //private static string _siteDescription;
        ///// <summary>
        ///// 站点描述
        ///// </summary>
        //public static string SiteDescription
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(_siteDescription))
        //            _siteDescription = GetSetting("SiteDescription");
        //        return _siteDescription;
        //    }
        //}
        //private static string _metaKeywords;
        ///// <summary>
        ///// 元数据关键字
        ///// </summary>
        //public static string MetaKeywords
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(_metaKeywords))
        //            _metaKeywords = GetSetting("MetaKeywords");
        //        return _metaKeywords;
        //    }
        //}
        //private static string _metaDescription;
        ///// <summary>
        ///// 元数据描述
        ///// </summary>
        //public static string MetaDescription
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(_metaDescription))
        //            _metaDescription = GetSetting("MetaDescription");
        //        return _metaDescription;
        //    }
        //}
        //private static string _siteAuthor;
        ///// <summary>
        ///// 站点作者
        ///// </summary>
        //public static string SiteAuthor
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(_siteAuthor))
        //            _siteAuthor = GetSetting("SiteAuthor");
        //        return _siteAuthor;
        //    }
        //}
        //private static string _theme;
        ///// <summary>
        ///// 站点主题
        ///// </summary>
        //public static string Theme
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(_theme))
        //            _theme = GetSetting("Theme");
        //        return _theme;
        //    }
        //}
        //private static string _version;
        ///// <summary>
        ///// 程序版本号
        ///// </summary>
        //public static string Version
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(_version))
        //            _version = GetSetting("Version");
        //        return _version;
        //    }
        //}
        //private static SiteStatus? _siteStatus;
        ///// <summary>
        ///// 站点状态
        ///// </summary>
        //public static SiteStatus SiteStatus
        //{
        //    get
        //    {
        //        if (_siteStatus == null)
        //            _siteStatus = (SiteStatus)int.Parse(GetSetting("SiteStatus")); //(SiteStatus)Enum.Parse(typeof(SiteStatus), GetSetting("SiteStatus"));
        //        return (SiteStatus)_siteStatus;
        //    }
        //}
        //private static string _copyright;
        ///// <summary>
        ///// 站点版权
        ///// </summary>
        //public static string Copyright
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(_copyright))
        //            _copyright = GetSetting("Copyright");
        //        return _copyright;
        //    }
        //}
        //private static string _charSet;
        ///// <summary>
        ///// 字符编码
        ///// </summary>
        //public static string CharSet
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(_charSet))
        //            _charSet = GetSetting("CharSet");
        //        return _charSet;
        //    }
        //}
        //private static string _siteIcp;
        ///// <summary>
        ///// 站点ICP
        ///// </summary>
        //public static string SiteIcp
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(_siteIcp))
        //            _siteIcp = GetSetting("SiteIcp");
        //        return _siteIcp;
        //    }
        //}
        //private static bool? _enableReg;
        ///// <summary>
        ///// 是否启用注册
        ///// </summary>
        //public static bool EnableReg
        //{
        //    get
        //    {
        //        if (_enableReg == null)
        //            _enableReg = GetSetting("EnableReg") == "1";
        //        return (bool)_enableReg;
        //    }
        //}
        //private static string _uploadRoot;
        ///// <summary>
        ///// 文件上传根路径
        ///// </summary>
        //public static string UploadRoot
        //{
        //    get
        //    {
        //        if (_uploadRoot == null)
        //            _uploadRoot = GetSetting("UploadRoot");
        //        return _uploadRoot;
        //    }
        //}
        //private static bool? _enableRss;
        ///// <summary>
        ///// 是否启用RSS
        ///// </summary>
        //public static bool EnableRss
        //{
        //    get
        //    {
        //        if (_enableRss == null)
        //            _enableRss = GetSetting("EnableRss") == "1";
        //        return (bool)_enableRss;
        //    }
        //}
        //private static int? _rssShowCount;
        ///// <summary>
        ///// RSS显示条数
        ///// </summary>
        //public static int RssShowCount
        //{
        //    get
        //    {
        //        if (_rssShowCount == null)
        //            _rssShowCount = int.Parse(GetSetting("RssShowCount"));
        //        return (int)_rssShowCount;
        //    }
        //}
        //private static RssShowType? _rssShowType;
        ///// <summary>
        ///// RSS显示类型
        ///// </summary>
        //public static RssShowType RssShowType
        //{
        //    get
        //    {
        //        if (_rssShowType == null)
        //            _rssShowType = (RssShowType)int.Parse(GetSetting("RssShowCount"));
        //        return (RssShowType)_rssShowType;
        //    }
        //}
        //private static string _smtpEmail;
        ///// <summary>
        ///// SMTP邮件
        ///// </summary>
        //public static string SmtpEmail
        //{
        //    get
        //    {
        //        if (_smtpEmail == null)
        //            _smtpEmail = GetSetting("SmtpEmail");
        //        return _smtpEmail;
        //    }
        //}
        //private static string _smtpServerUrl;
        ///// <summary>
        ///// SMTP服务器地址
        ///// </summary>
        //public static string SmtpServerUrl
        //{
        //    get
        //    {
        //        if (_smtpServerUrl == null)
        //            _smtpServerUrl = GetSetting("SmtpServerUrl");
        //        return _smtpServerUrl;
        //    }
        //}
        //private static string _smtpServerPort;
        ///// <summary>
        ///// SMTP服务端端口
        ///// </summary>
        //public static string SmtpServerPort
        //{
        //    get
        //    {
        //        if (_smtpServerPort == null)
        //            _smtpServerPort = GetSetting("SmtpServerPort");
        //        return _smtpServerPort;
        //    }
        //}
        //private static string _smtpUserName;
        ///// <summary>
        ///// SMTP邮箱用户名
        ///// </summary>
        //public static string SmtpUserName
        //{
        //    get
        //    {
        //        if (_smtpUserName == null)
        //            _smtpUserName = GetSetting("SmtpUserName");
        //        return _smtpUserName;
        //    }
        //}
        //private static string _smtpPassword;
        ///// <summary>
        ///// SMTP邮箱密码
        ///// </summary>
        //public static string SmtpPassword
        //{
        //    get
        //    {
        //        if (_smtpPassword == null)
        //            _smtpPassword = GetSetting("SmtpPassword");
        //        return _smtpPassword;
        //    }
        //}
        //private static bool? _smtpEnableSsl;
        ///// <summary>
        ///// SMTP邮箱是否启用SSL安全连接
        ///// </summary>
        //public static bool SmtpEnableSsl
        //{
        //    get
        //    {
        //        if (_smtpEnableSsl == null)
        //            _smtpEnableSsl = GetSetting("SmtpEnableSsl") == "1";
        //        return (bool)_smtpEnableSsl;
        //    }
        //}
        //private static IDictionary<string, string> _settingDict;
        ///// <summary>
        ///// 设置信息字典
        ///// </summary>
        //public static void SetDict(IDictionary<string, string> setting)
        //{
        //    Setting._settingDict = setting;
        //}
        ///// <summary>
        ///// 获取设置信息
        ///// </summary>
        ///// <param name="name">设置名称</param>
        ///// <returns>设置值</returns>
        //public static string GetSetting(string name)
        //{
        //    return _settingDict != null ? _settingDict[name] : string.Empty;
        //} 
        #endregion

        #region 读库配置

        #region 方法
        /// <summary>
        /// 配置信息字典
        /// </summary>
        private static IDictionary<string, string> _dict;

        /// <summary>
        /// 获取设置信息
        /// </summary>
        /// <param name="name">设置名称</param>
        /// <returns>设置值</returns>
        public static string Get(string name)
        {
            if (_dict == null) return string.Empty;
            return _dict.ContainsKey(name) ? _dict[name] : string.Empty;
        }

        /// <summary>
        /// 获取设置信息Int类型值
        /// </summary>
        /// <param name="name">设置名称</param>
        /// <param name="def">缺省值</param>
        /// <returns>设置值</returns>
        public static int GetInt(string name, int def)
        {
            string res = Get(name);
            int.TryParse(res, out def);
            return def;
        }

        /// <summary>
        /// 初始化信息字典
        /// </summary>
        /// <param name="dict">设置信息字段</param>
        public static void Init(IDictionary<string, string> dict)
        {
            lock ("SettingDictLockHelper")
            {
                _dict = dict;
            }
        }

        /// <summary>
        /// 移除单个设置
        /// </summary>
        /// <param name="key">键</param>
        public static void Remove(string key)
        {
            lock ("SettingDictLockHelper")
            {
                _dict.Remove(key);
            }
        }

        /// <summary>
        /// 移除多个设置
        /// </summary>
        /// <param name="keys">键</param>
        public static void Remove(params string[] keys)
        {
            lock ("SettingDictLockHelper")
            {
                foreach (var key in keys)
                {
                    _dict.Remove(key);
                }
            }
        }

        /// <summary>
        /// 修改信息字典值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public static void Set(string key, string value)
        {
            lock ("SettingDictLockHelper")
            {
                if (_dict.ContainsKey(key))
                    _dict[key] = value;
                else
                    _dict.Add(key, value);
            }
        }
        #endregion

        #region 常用设置封装

        #region 系统信息

        /// <summary>
        /// 管理平台区域名(目录模式为目录名,二级域名模式为二级域名)
        /// </summary>
        public static string AdminAreaName { get { return Get("admin_area"); } }

        #endregion

        #region 站点信息设置

        /// <summary>
        /// 生产者(Generator)
        /// </summary>
        public static string Generator { get { return "Wedn.Net"; } }
        /// <summary>
        /// 当前程序版本号
        /// </summary>
        public static string Version { get { return Get("version"); } }
        /// <summary>
        /// 站点URL
        /// </summary>
        public static string SiteUrl { get { return Get("site_url"); } }
        /// <summary>
        /// 站点名称
        /// </summary>
        public static string SiteName { get { return Get("site_name"); } }
        /// <summary>
        /// 站点描述
        /// </summary>
        public static string SiteDescription { get { return Get("site_description"); } }
        /// <summary>
        /// 站点流量数
        /// </summary>
        public static int SiteViewCount { get { return GetInt("site_view_count", 0); } }
        /// <summary>
        /// 站点文章数
        /// </summary>
        public static int SitePostCount { get { return GetInt("site_post_count", 0); } }
        /// <summary>
        /// 站点分类数
        /// </summary>
        public static int SiteCategoryCount { get { return GetInt("site_category_count", 0); } }
        /// <summary>
        /// 站点标签数
        /// </summary>
        public static int SiteTagCount { get { return GetInt("site_tag_count", 0); } }
        /// <summary>
        /// 站点评论数
        /// </summary>
        public static int SiteCommentCount { get { return GetInt("site_comment_count", 0); } }
        /// <summary>
        /// 站点图标
        /// </summary>
        public static string SiteFavicon { get { return Get("site_favicon"); } }
        /// <summary>
        /// 备案ICP
        /// </summary>
        public static string SiteIcp { get { return Get("site_icp"); } }
        /// <summary>
        /// 版权声明
        /// </summary>
        public static string Copyright { get { return Get("copyright"); } }
        /// <summary>
        /// 站长统计代码
        /// </summary>
        public static string StatisticCode { get { return Get("statistic_code"); } }
        /// <summary>
        /// META关键字
        /// </summary>
        public static string MetaKeywords { get { return Get("meta_keywords"); } }
        /// <summary>
        /// META描述
        /// </summary>
        public static string MetaDescription { get { return Get("meta_description"); } }
        /// <summary>
        /// META作者
        /// </summary>
        public static string MetaAuthor { get { return Get("meta_author"); } }

        #endregion

        #region 功能设置

        /// <summary>
        /// 站点状态
        /// </summary>
        public static SiteStatus SiteStatus { get { return (SiteStatus)GetInt("site_status", 0); } }
        /// <summary>
        /// 站点评论状态
        /// </summary>
        public static CommentStatus CommentStatus { get { return (CommentStatus)GetInt("comment_status", 0); } }
        /// <summary>
        /// 站点语言
        /// </summary>
        public static string Culture { get { return Get("site_culture"); } }
        /// <summary>
        /// 字符集编码
        /// </summary>
        public static string Charset { get { return Get("site_charset"); } }
        /// <summary>
        /// 是否允许注册
        /// </summary>
        public static bool EnableRegister { get { return Get("enable_register").ToLower() == "true"; } }
        /// <summary>
        /// 上传文件根目录
        /// </summary>
        public static string UploadRoot { get { return Get("upload_root"); } }
        /// <summary>
        /// 缓存文章条数
        /// </summary>
        public static int PostCacheSize { get { return GetInt("post_cache_size", 20); } }
        /// <summary>
        /// 分页显示条数
        /// </summary>
        public static int PageSize { get { return GetInt("page_size", 10); } }
        /// <summary>
        /// 用户头像显示类型
        /// </summary>
        public static string UserAvatar { get { return Get("user_avatar"); } }
        /// <summary>
        /// 电脑端主题设置
        /// </summary>
        public static string Theme { get { return Get("theme"); } }
        /// <summary>
        /// 手机端主题设置
        /// </summary>
        public static string MobileTheme { get { return Get("mobile_theme"); } }
        /// <summary>
        /// 首页显示文章类型
        /// </summary>
        public static PostShowType PostShowType { get { return (PostShowType)GetInt("post_show_type", 0); } }
        /// <summary>
        /// RSS显示文章类型
        /// </summary>
        public static RssShowType RssShowType { get { return (RssShowType)GetInt("rss_show_type", 0); } }
        /// <summary>
        /// 默认边栏小工具
        /// </summary>
        public static string DefaultWidget { get { return Get("default_widget"); } }
        /// <summary>
        /// 列表页边栏小工具
        /// </summary>
        public static string ListWidget { get { return Get("list_widget"); } }
        /// <summary>
        /// 详细页边栏小工具
        /// </summary>
        public static string DetailWidget { get { return Get("detail_widget"); } }
        /// <summary>
        /// 全局固定连接
        /// </summary>
        public static string Permalink { get { return Get("permalink"); } }
        /// <summary>
        /// 全局伪静态拓展名
        /// </summary>
        public static string RewriteExtension { get { return Get("rewrite_extension"); } }
        /// <summary>
        /// 全局垃圾词汇
        /// </summary>
        public static string CommentSpamWords { get { return Get("comment_spam_words"); } }
        /// <summary>
        /// 是否显示错误页
        /// </summary>
        public static bool ShowErrorPage { get { return Get("show_error_page").ToLower() == "true"; } }
        /// <summary>
        /// OAuth腾讯QQ应用Id
        /// </summary>
        public static string OAuthQQAppId { get { return Get("oauth_qq_appid"); } }

        #endregion

        #region SMTP邮件设置

        /// <summary>
        /// SMTP邮件服务器地址
        /// </summary>
        public static string SmtpServer { get { return Get("smtp_server"); } }
        /// <summary>
        /// SMTP邮件服务器端口
        /// </summary>
        public static int SmtpServerPort { get { int def; return int.TryParse(Get("smtp_server_port"), out def) ? def : 25; } }
        /// <summary>
        /// SMTP是否启用SSL安全链接
        /// </summary>
        public static bool SmtpEnableSsl { get { return Get("smtp_enablessl").ToLower() == "true"; } }
        /// <summary>
        /// SMTP账户用户名
        /// </summary>
        public static string SmtpUserName { get { return Get("smtp_username"); } }
        /// <summary>
        /// SMTP账户密码
        /// </summary>
        public static string SmtpPassword { get { return Get("smtp_password"); } }

        #endregion

        #endregion

        #endregion

        #region 常用设置

        #region 当前主题绝对路径 +static string CurrentThemePath
        /// <summary>
        /// 当前主题绝对路径
        /// </summary>
        public static string CurrentThemePath
        {
            get
            {
                //if (string.IsNullOrEmpty(_currentThemePath))
                //    _currentThemePath = string.Format("{0}/{1}/{2}", Config.SiteUrl, Config.ThemeRoot, Theme);// (Config.SiteUrl + Config.ThemeRoot.Trim('/') + "/" + Theme + "/").ToLower();
                return string.Format("{0}{1}{2}/", Config.SiteUrl, Config.ThemeRoot.TrimStart('/'), Theme).ToLower();
            }
        }
        #endregion

        #endregion

        #region 常用方法

        #region 获取主题配置项的私有Key +static string GetThemeOptionKey(string name)
        /// <summary>
        /// 获取主题配置项的私有Key
        /// </summary>
        /// <param name="name">选项名称</param>
        /// <returns>选项私有Key</returns>
        public static string GetThemeOptionKey(string name)
        {
            return string.Format("_{0}_{1}", Theme.ToLower(), name);
        }
        #endregion

        #endregion

        #region Temp

        public static string AdminContentRoot =  @"/Content/";

        #endregion
    }
}
